
var ajax_pagination = {};
;
ajax_pagination.CommonPagination = {
	fit: function(portlet){
	    var pgn = portlet.find('.commonPagination');
	    if(pgn.height()>30)
	        pgn.addClass('compactPagination');
	},

setup: function(params){
   
    var id = params.id;
    var viewmore = params.viewmore;
    var reload = params.reload;

    var portlet = $('#' + id);
    if(window.frames.Main && $(window).index(portlet) == -1){
        var portlet = $('#'+id,$(window.frames['Main'].document));
    }
    var pagingInfo =  portlet.find('.pagingInfo');
   
    if(pagingInfo.length==0)return

    var intAttr = function(name){
        return parseInt(pagingInfo.attr(name));
    };
    var startRow = intAttr('startRow');
    var endRow = intAttr('endRow');
    var startDisplayPage = intAttr('startDisplayPage');
    var endDisplayPage = intAttr('endDisplayPage');
    var totalPages = intAttr('totalPages');
    var totalRows = intAttr('totalRows');
    var currentPage = intAttr('currentPage');
   
    //adjust pagination
    //ajax_pagination.CommonPagination.fit(portlet)

    //assign value
    with(portlet){
        find('li.page input').val(currentPage);
        find('li.pageCount strong').text(totalPages);
        if(viewmore && viewmore.title)
            find('.inlineFooter a').html(viewmore.title + '<span>&#187;</span>').click(viewmore.event);
        else
            find('.inlineFooter').remove();
        find('.listPaginationInfo').text('Displaying items ' + startRow + '-' + endRow + ' of ' + totalRows);
    }
   
    //bind btn event
    var roll = function(e){
        var toPage = e.data?e.data.toPage:null;
        if(!toPage){
            var btn = $(this);
            if(btn.hasClass('next'))
                toPage = currentPage+1;
            else if(btn.hasClass('previous'))
                toPage = currentPage-1;
            else if(btn.hasClass('first'))
                toPage = 1;
            else if (btn.hasClass('last'))
                toPage = totalPages;
            else
                return
        }

        if(toPage>0 && toPage<=totalPages && toPage!=currentPage && reload){
            reload.params.start = (toPage-1) * reload.params.limit;
            reload.event(reload.params);
        }
    };
    portlet.find( 'li.previous, li.next, li.first, li.last' ).click( roll );
   
    //bind input event
    var filterKey = function(e){
        if ( ( e.keyCode < 48 || e.keyCode > 57 ) && ( e.keyCode < 96 || e.keyCode > 105 ) && ( e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 39 ) ) e.preventDefault();
    };
    var jump = function(e){
        if ( e.keyCode == 13 ){
            e.data = {toPage: $(this).val()};
            roll(e);
        }
    };
    portlet.find( 'li.page input' )
        .keydown(filterKey)
        .keyup(jump);
}
};


//common portlet paging
lemon = {
		loadDatas : function(params) {
			var url = params.url + "?start=" + params.start + "&limit="
					+ params.limit;
			var $portlet_content = $('#'+params.portletId + " .portlet-content");
			$portlet_content.mask('Loading...');
			$portlet_content.load(url, function() {
				lemon.setup(params);
				$portlet_content.unmask();
			});
		},
		//dynamic modify pagination
		setup : function(params) {
			ajax_pagination.CommonPagination.setup({
				id : params.portletId,
				//viewmore: {title: 'More' , event: function(){}},
				reload : {
					event : lemon.loadDatas,
					params : params
				}
			});
			//TEST.CommonPagination.fit($("#" + params.portletId));
		}
	};
